package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

//java数据库编程 
public class JDBCUtil {

	private static String driverName = "com.mysql.cj.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useSSL=false";
	private static String userName = "root";
	private static String password = "password";
   
          public static  Connection connectiontt(){
            try {
                 Class.forName("com.mysql.cj.jdbc.Driver");            
                 Connection conn = DriverManager.getConnection(url, userName, password);
                 return conn;
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "驱动连接错误");
            }
            return null;
            
        }
	public static void registerDriver() {
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("驱动未加载！");
		}
	}

	public static Connection getConnection() throws SQLException {
		Connection conn = null;
		System.out.println("正在连接...");
		conn = DriverManager.getConnection(url, userName, password);
		System.out.println("连接成功!");
		return conn;

	}

	public static void closeConnection(Connection conn) {
		System.out.println("关闭连接...");
		try {
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void realeaseAll(ResultSet rs,Statement st,Connection conn){
		if(rs!=null){
			try {
				rs.close();
				rs = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (st!=null){
			try {
				st.close();
				st = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		closeConnection(conn);
	}
	
	
		
	
}


